<?php
namespace app\modules\developer\models;
use common\component\tool\Tool;
use app\models\Basic;

use Yii;

class AppInfo extends Basic
{
    public $app_intranet_agreement;//域协议
    public $app_outside_agreement;//域协议
    
    public static function tableName()
    {
        return '{{%app_info}}';
    }

    public function rules()
    {
        return [
            ['app_cat_id', 'default', 'value' => 0],
            ['app_cat_id', 'integer', 'message' => 'app_cat_id必须为数字，或数字字符串', 
                           'min'=>0, 'tooSmall' => 'app_cat_id最小存储0', 
                           'max'=>65535, 'tooBig' => 'app_cat_id最大存储65535'],
            ['app_name', 'required', 'message' => 'app_name不能为空', 'on' => 'add'],
            ['app_name', 'string', 'message' => 'app_name必须是字符串',
                         'min' => 2, 'tooShort' => 'app_name最短2位',
                         'max' => 16, 'tooLong' => 'app_name最长16位'],
            ['describe', 'default', 'value' => ''],
            ['describe', 'string', 'message' => 'describe必须是字符串',
                         'max' => 50, 'tooLong' => 'describe最长50位'],
            ['icon', 'default', 'value' => ''],
            ['icon', 'string', 'message' => 'icon必须是字符串', 
                     'max' => 32, 'tooLong' => 'icon最长32位'],
            ['app_intranet_agreement', 'in', 'range' => array("http://", "https://"), 'message' => 'app_intranet_agreement值必须在http://,https://中选择'],
            ['app_outside_agreement', 'in', 'range' => array("http://", "https://"), 'message' => 'app_outside_agreement值必须在http://,https://中选择'],
            ['app_intranet_url', 'default', 'value' => ''],
            ['app_intranet_url', 'string', 'message' => 'app_intranet_url必须是字符串', 
                                           'max' => 32, 'tooLong' => 'app_intranet_url最长32位'],
            ['app_outside_url', 'default', 'value' => ''],
            ['app_outside_url', 'string', 'message' => 'app_outside_url必须是字符串', 
                                          'max' => 32, 'tooLong' => 'app_outside_url最长32位'],
            ['preview', 'default', 'value' => ''],
            ['preview', 'string', 'message' => 'preview必须是字符串', 
                        'max' => 512, 'tooLong' => 'preview最长512位'],          
            ['outside', 'default', 'value' => 1],
            ['outside', 'integer', 'message' => 'outside必须为数字，或数字字符串'],
            ['outside', 'in', 'range' => array(1, 2), 'message' => 'outside值必须在1,2中选择'],
            ['url_target', 'default', 'value' => 1],
            ['url_target', 'integer', 'message' => 'url_target必须为数字，或数字字符串'],
            ['url_target', 'in', 'range' => array(1, 2), 'message' => 'url_target值必须在1,2中选择'],
            ['sort', 'integer', 'message' => 'sort必须为数字，或数字字符串', 
                     'min'=>0, 'tooSmall' => 'sort最小存储0', 
                     'max'=>255, 'tooBig' => 'sort最大存储255'],
            ['display', 'default', 'value' => 1],
            ['display', 'integer', 'message' => 'display必须为数字，或数字字符串'],
            ['display', 'in', 'range' => array(1, 2), 'message' => 'display值必须在1,2中选择'],
        ];
    }


    /*添加信息*/
    public function addAppInfo($data)
    {
        if(isset($data['AppInfo']['app_intranet_agreement']) 
            and isset($data['AppInfo']['app_intranet_url'])
            and !empty($data['AppInfo']['app_intranet_url'])
            ){
            $data['AppInfo']['app_intranet_url'] = $data['AppInfo']['app_intranet_agreement'] . $data['AppInfo']['app_intranet_url'];
        }
        if(isset($data['AppInfo']['app_outside_agreement']) 
            and isset($data['AppInfo']['app_outside_url'])
            and !empty($data['AppInfo']['app_outside_url'])
            ){
            $data['AppInfo']['app_outside_url'] = $data['AppInfo']['app_outside_agreement'] . $data['AppInfo']['app_outside_url'];
        }
        if(isset($data['AppInfo']['preview']) and is_array($data['AppInfo']['preview'])){
            $data['AppInfo']['preview'] = Tool::changeUploadFormat($data['AppInfo']['preview']);
        }
        if(isset($data['AppInfo']['outside'])){
            $data['AppInfo']['outside'] = Tool::changeBool($data['AppInfo']['outside']);
        }
        if(isset($data['AppInfo']['display'])){
            $data['AppInfo']['display'] = Tool::changeBool($data['AppInfo']['display']);
        }
        // P($data);
        $this->scenario = 'add';
        if($this->load($data) and $this->validate()){
            if($this->save(false)){
                $app_info_id = $this->getPrimaryKey();
                $this->updateAll(['sort' => $app_info_id], 'app_info_id = :id', [':id' => $app_info_id]);
                return $app_info_id;
            }
        }
        return false;
    }

    /*
    修改信息
    $data   修改的数据
    $id     信息ID
    $flag   值为setDisplay时 作用是更改是否显示，为空是修改菜单
    */
    public function modifyAppInfo($data, $id, $flag = '')
    {
        // P($data);
        if(isset($data['AppInfo']['app_intranet_agreement']) 
            and isset($data['AppInfo']['app_intranet_url'])
            and !empty($data['AppInfo']['app_intranet_url'])
            ){
            $data['AppInfo']['app_intranet_url'] = $data['AppInfo']['app_intranet_agreement'] . $data['AppInfo']['app_intranet_url'];
        }
        if(isset($data['AppInfo']['app_outside_agreement']) 
            and isset($data['AppInfo']['app_outside_url'])
            and !empty($data['AppInfo']['app_outside_url'])
            ){
            $data['AppInfo']['app_outside_url'] = $data['AppInfo']['app_outside_agreement'] . $data['AppInfo']['app_outside_url'];
        }
        if(isset($data['AppInfo']['preview'])){
            if(is_array($data['AppInfo']['preview'])){
                $data['AppInfo']['preview'] = Tool::changeUploadFormat($data['AppInfo']['preview']);
            }
        }else{
            $data['AppInfo']['preview'] = '';
        }
        // P($data);
        if(isset($data['AppInfo']['outside'])){
            $data['AppInfo']['outside'] = Tool::changeBool($data['AppInfo']['outside']);
        }
        if(isset($data['AppInfo']['display'])){
            $data['AppInfo']['display'] = Tool::changeBool($data['AppInfo']['display']);
        }
        if($flag != 'setDisplay'){
            $this->scenario = 'add';
        }
        $appInfo = AppInfo::find()->where('app_info_id = :id', [':id' => $id])->one();
        // P($appInfo);
        if(!is_null($appInfo)){
            if($this->load($data) and $this->validate()){
                // P($data);
                if(!empty($this->app_cat_id) and empty($flag)){
                    $appInfo->app_cat_id = $this->app_cat_id;
                }
                if(!empty($this->app_name) and empty($flag)){
                    $appInfo->app_name = $this->app_name;
                }
                if(!empty($this->describe) and empty($flag)){
                    $appInfo->describe = $this->describe;
                }
                if(!empty($this->icon) and empty($flag)){
                    $appInfo->icon = $this->icon;
                }
                if(empty($flag)){
                    $appInfo->app_intranet_url = $this->app_intranet_url;
                }
                if(empty($flag)){
                    $appInfo->app_outside_url = $this->app_outside_url;
                }
                if(isset($this->preview) and empty($flag)){
                    $appInfo->preview = $this->preview;
                }
                if(!empty($this->outside) and empty($flag)){
                    $appInfo->outside = $this->outside;
                }
                if(!empty($this->url_target) and empty($flag)){
                    $appInfo->url_target = $this->url_target;
                }
                if(!empty($this->display)){
                    $appInfo->display = $this->display;
                }
                if($appInfo->save(false)){
                    return true;
                }
            }
        };
        return false;
    }

    /*关联查询 分类信息*/
    public function getCategory()
    {
        $category = $this->hasOne(AppCategory::className(), ['app_cat_id' => 'app_cat_id'])
                    ->select(['app_cat_id', 'cat_name', 'icon']);
        return $category;
    }

    /*关联查询 价格*/
    public function getPrice()
    {
        $price = $this->hasMany(AppPrice::className(), ['app_info_id' => 'app_info_id']);
        return $price;
    }

    
}